package util;

import org.apache.commons.lang3.StringUtils;

import javax.net.ssl.HttpsURLConnection;
import javax.net.ssl.SSLContext;
import javax.net.ssl.TrustManager;
import java.net.HttpURLConnection;
import java.net.URL;

import static util.HttpUtil.myX509TrustManager;

/**
 * @author zhanglinfeng
 * @date create in 2022/12/13 14:43
 */
public class ImageUtils {
    public static String handleImages(String detailStyle, String infoUrl) {
        if (StringUtils.isNotEmpty(detailStyle)) {
            int imgBeginIndex = 0;
            int imgEndIndex = 0;
            while (true) {
                imgBeginIndex = detailStyle.indexOf("<img", imgEndIndex);
                imgEndIndex = detailStyle.indexOf(">", imgBeginIndex);
                if (imgBeginIndex == -1 || imgEndIndex == -1) {
                    break;
                }
                String imgUrl = detailStyle.substring(imgBeginIndex, imgEndIndex + 1);
                if (!imgUrl.contains("width=")) {
                    StringBuilder stringBuilder = new StringBuilder(detailStyle);
                    stringBuilder.insert(imgEndIndex, " width=\"100%\" ");
                    detailStyle = stringBuilder.toString();
                }
                int beginIndex = imgUrl.indexOf("src=\"");
                int endIndex = imgUrl.indexOf("\"", beginIndex + 5);
                String imageUrl = imgUrl.substring(beginIndex + 5, endIndex);
                String newImageUrl = imageUrl;
                if (imageUrl.startsWith("http")) {
                    continue;
                }
                while (newImageUrl.startsWith(".") || newImageUrl.startsWith("/")) {
                    newImageUrl = newImageUrl.substring(1);
                }
                String[] infoUrlArr = infoUrl.split("/");
                StringBuilder infoUrlPrefix = new StringBuilder(infoUrlArr[0] + "//");
                for (int j = 2; j < infoUrlArr.length; j++) {
                    infoUrlPrefix.append(infoUrlArr[j]).append("/");
                    if (ImageUtils.isImagesTrue(infoUrlPrefix + newImageUrl)) {
                        detailStyle = detailStyle.replace(imageUrl, infoUrlPrefix + newImageUrl);
                        break;
                    }
                }
            }
        }
        return detailStyle;
    }

    public static boolean isImagesTrue(String url) {
        try {
            Thread.sleep(1000);
            URL uri = new URL(url);
            HttpURLConnection conn = (HttpURLConnection) uri.openConnection();
            if (url.startsWith("https")) {
                SSLContext sslcontext = SSLContext.getInstance("TLS");
                sslcontext.init(null, new TrustManager[]{myX509TrustManager}, null);
                ((HttpsURLConnection) conn).setSSLSocketFactory(sslcontext.getSocketFactory());
            }
            conn.setConnectTimeout(5000);
            conn.setReadTimeout(5000);
            conn.setRequestMethod("HEAD");
            conn.setRequestProperty("User-Agent", AgentUtil.get());
            return conn.getResponseCode() == HttpURLConnection.HTTP_OK;
        } catch (Exception e) {
            return false;
        }
    }


}
